<?php

namespace app\shop\controller\projects;

use think\Controller;

class Project extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business.Business');
        $this->OrderModel = model('Project.Order');
        $this->ProjectModel = model('Project.Project');
    }

    public function index()
    {
        if($this->request->isPost())
        {
            $page = $this->request->param('page', 1, 'trim');
            $status = $this->request->param('status', 'all', 'trim');
            $keywords = $this->request->param('keywords', '', 'trim');

            $limit = 10;
            $start = ($page-1)*$limit;

            $where = [];

            if($status != "all")
            {
                $where['status'] = $status;
            }
            if(!empty($keywords))
            {
                $where['title']=['LIKE',"%$keywords%"];
            }

            $list = $this->ProjectModel->where($where)->limit($start, $limit)->select();

            if($list)
            {
                $this->success('活动列表', null, $list);
                exit;
            }else
            {
                $this->error('暂无更多数据');
                exit;
            }
        }
    }

    public function info()
    {
        if($this->request->isPost())
        {
            $id = $this->request->param('id', 0, 'trim');
            $busid = $this->request->param('busid', 0, 'trim');

            $info = $this->ProjectModel->find($id);

            $info['order']=false;

            if($busid)
            {
                $business=$this->BusinessModel->find($busid);
                if(!$business)
                {
                    $this->error('请先登录');
                    exit;
                }
                $where=[
                    'pid'=>$id,
                    'busid'=>$busid
                ];
                $order=$this->OrderModel->where($where)->find();
                $order ? $info['order']=true : $info['order']=false; 
            }
            // $order = $this->OrderModel->where()

            if(!$info)
            {
                $this->error('暂无活动数据');
                exit;
            }

            $this->success('返回活动数据', null, $info);
            exit;
        }
    }

    // 浏览量增加
    public function addview()
    {
        if($this->request->isPost())
        {
            $pid=$this->request->param('pid',0,'trim');
            $project=$this->ProjectModel->find($pid);
            if(!$project)
            {
                $this->error('活动不存在');
                exit;
            }
            $view=$project['view'];
            $update=bcadd($view,1);

            $data=[
                'id'=>$pid,
                'view'=>$update
            ];
            $result=$this->ProjectModel->isUpdate(true)->save($data);
            if($result === FALSE)
            {
                $this->error('浏览量增加失败');
                exit;
            }
            $this->success('浏览+1');
            exit;
        }
    }
}
